// This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International  
// https://creativecommons.org/licenses/by-nc-sa/4.0/
// © BigBeluga

//@version=6
indicator("HTF Frequency Zone [BigBeluga]", overlay = true, max_lines_count = 500, calc_bars_count = 3000)

// ＩＮＰＵＴＳ ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{

tf    = input.string("W", "Timeframe", ["D", "W", "M"])
colUp = input.color(color.lime, "", inline = "c")
colDn = input.color(color.orange, "", inline = "c")
colPC = input.color(color.red, "", inline = "c")

var H           = 0.
var L           = low
var VZ          = 0.
var barSize     = 0
buy             = 0
sell            = 0
var valueStart  = 0.
var start       = 0
// }

// ＣＡＬＣＵＬＡＴＩＯＮＳ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{


if timeframe.change(tf)

    col = close > valueStart[1] ? colUp : colDn
    

    if barSize > 10
        lh = line.new(start, H, bar_index, H, style = line.style_dotted, color = col)
        ll = line.new(start, L, bar_index, L, style = line.style_dotted, color = col)
        line.new(start, VZ, bar_index, VZ, style = line.style_dotted, color = col)

        linefill.new(lh, ll, color.new(col, 95))


    H       := 0.
    L       := low
    VZ      := 0
    valueStart := 0
    
    buy  := 0
    sell := 0

    barSize := bar_index - start

    barSize := barSize >= 500 ? 500 : barSize

    start   := bar_index
    valueStart := close

// ＰＬＯＴ ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{

if barSize > 10

    value = 0

    for i = 0 to bar_index - start

        H    := math.max(H, high[i])
        L    := math.min(L, low[i])
        buy  += close[i] > open[i] ? 1 : 0
        sell += close[i] < open[i] ? 1 : 0

    line.delete(line.new(start, H, start+barSize, H, style = line.style_dashed, color = colDn)[1])
    line.delete(line.new(start, L, start+barSize, L, style = line.style_dashed, color = colUp)[1])

    step = (H - L) / 100

    bins = array.new<int>(100, 0)

    for i = 0 to bar_index - start

        cls = close[i]

        for k = 0 to 100-1

            loww = L + step * k
            mid  = loww + step/2

            if math.abs(cls - mid) < step 
                bins.set(k, bins.get(k) + 1)

    poc  = L + step * bins.indexof(bins.max())
    VZ  := poc
    line.delete(line.new(start, poc, start+barSize, poc, style = line.style_solid, color = colPC, width = 2)[1])

    txtTF = switch tf 
        "D" => "Daily: "
        "W" => "Weekly: "
        "M" => "Monthly: "
        
    label.delete(label.new(start+barSize, VZ, txtTF + str.tostring(bins.max()), style = label.style_label_left, color = colPC)[1])
    label.delete(label.new(start+barSize, H, str.tostring(sell), style = label.style_label_left, color = color.new(colDn, 50))[1])
    label.delete(label.new(start+barSize, L, str.tostring(buy ), style = label.style_label_left, color = color.new(colUp, 50))[1])
else if barstate.islast
    label.delete(label.new(bar_index, hl2, "Change Chart timeframe to lower\nor indicator's timeframe to higher ⚠️", style = label.style_label_left, color = color.orange)[1])



// }
